package com.brewster.config.model;

import java.util.List;

import com.jfinal.plugin.activerecord.Model;

/**
 * 本 demo 仅表达最为粗浅的 jfinal 用法，更为有价值的实用的企业级用法 详见 JFinal 俱乐部:
 * http://jfinal.com/club
 * 
 * Blog model. 数据库字段名建议使用驼峰命名规则，便于与 java 代码保持一致，如字段名： userId
 */
@SuppressWarnings("serial")
public class Account extends Model<Account> {
	public static final Account dao = new Account();
	public List<String> permission;

	public List<String> getPermission() {
		return permission;
	}

	public void setPermission(List<String> permission) {
		this.permission = permission;
	}

	public void setPassword(java.lang.String password) {
		set("password", password);
	}

	public java.lang.String getPassword() {
		return get("password");
	}

	public void setSalt(java.lang.String salt) {
		set("salt", salt);
	}

	public java.lang.String getSalt() {
		return get("salt");
	}

	public void setStatus(java.lang.Integer status) {
		set("status", status);
	}

	public void setAccount(java.lang.String userName) {
		set("account", userName);
	}

	public java.lang.String getAccount() {
		return get("account");
	}

	public Account login(String account, String password, boolean keepLogin, String loginIp) {
		account = account.toLowerCase().trim();
		password = password.trim();
		Account loginAccount = dao.findFirst(
				"select t.*,group_concat(t1.role_id separator \",\") role_id from da_account t left join da_account_role t1 on t.id = t1.account_id where account = ? ",
				account);
		if (loginAccount == null || loginAccount.getInt("id") == null) {
			Account loginAccount1 = Account.dao.findFirst(
					"select t.*,group_concat(t1.role_id separator \",\") role_id from da_account t left join da_account_role t1 on t.id = t1.account_id where phone = ?",
					account);
			if (loginAccount1 == null || loginAccount1.getInt("id") == null) {
				Account loginAccount2 = Account.dao.findFirst(
						"select t.*,group_concat(t1.role_id separator \",\") role_id from da_account t left join da_account_role t1 on t.id = t1.account_id where email = ?",
						account);
				if (loginAccount2 == null || loginAccount2.getInt("id") == null) {
					return null;
				} else {
					return loginAccount2;
				}
			} else {
				return loginAccount1;
			}
		} else {
			return loginAccount;
		}
	}
}
